package com.lds.erp.index;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lds.erp.user.user.UserManageFacade;
import com.lds.erp.user.user.result.UserDetailsQueryResult;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dph.common.utils.base.PageEntity;
import com.dph.common.utils.utils.AppUtils;
import com.lds.erp.base.JsonResult;
import com.lds.erp.base.banner.AppIndexCatFacade;
import com.lds.erp.base.banner.BannerManageFacade;
import com.lds.erp.base.banner.info.BannerPageQueryInfo;
import com.lds.erp.base.banner.info.BsAppIndexCatInfo;
import com.lds.erp.base.banner.order.BannerListQueryOrder;
import com.lds.erp.base.banner.order.BannerProductQueryOrder;
import com.lds.erp.base.banner.order.BsAppIndexCatPageQueryOrder;
import com.lds.erp.base.banner.result.BannerListQueryResult;
import com.lds.erp.base.banner.result.BannerProductQueryResult;
import com.lds.erp.base.banner.result.BsAppIndexCatPageQueryResult;
import com.lds.erp.common.BannerTypeEnum;
import com.lds.erp.common.PtSkuLabelEnum;
import com.lds.erp.common.YNEnum;
import com.lds.erp.index.vo.BannerCatVo;
import com.lds.erp.index.vo.BannerPageQueryInfoVo;
import com.lds.erp.index.vo.BsAppHorCatVo;
import com.lds.erp.index.vo.BsAppIndexCatVo;
import com.lds.erp.index.vo.BsBannerProductQueryVo;
import com.lds.erp.util.JsonUtils;
import com.lds.erp.util.QiNiuTokenVO;
import com.lds.erp.util.Uptoken;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author ： fjl
 * @date ： 2019/1/21/021 11:08
 */
@Api(value = "banner以及广告列表", description = "banner以及广告列表", tags = { "banner以及广告列表" })
@RestController
@RequestMapping(value = "/api/banner/")
public class BannerController {

	@Reference(version = "1.0")
	private BannerManageFacade bannerManageFacade;

	@Reference(version = "1.0")
	private AppIndexCatFacade appIndexCatFacade;

	@Reference(version = "1.0")
	UserManageFacade userManageFacade;

	@GetMapping(value = "bannerListQuery")
	@ApiOperation(value = "小程序首页banner广告及分类", notes = "小程序首页banner广告及分类")
	public JsonResult<BannerCatVo> bannerListQuery(String  districtId) {

		BannerListQueryOrder order = new BannerListQueryOrder();
		order.setBannerType(BannerTypeEnum.BANNER);
		order.setSerialNo(AppUtils.newNo());
		order.setDistrictId(districtId);

		// 查询banner 信息
		BannerListQueryResult bannerResult = bannerManageFacade.bannerListQuery(order);

		BannerCatVo queryVo = new BannerCatVo();

		List<BannerPageQueryInfoVo> bannerList = new ArrayList<>();
		List<BsAppIndexCatVo> catList = new ArrayList<>();

		if (bannerResult.getList().size() > 0) {
			List<BannerPageQueryInfo> tempList = bannerResult.getList();
			for (BannerPageQueryInfo bannerPageQueryInfo : tempList) {
				BannerPageQueryInfoVo vo = new BannerPageQueryInfoVo();
				BeanUtils.copyProperties(bannerPageQueryInfo, vo);
				bannerList.add(vo);
			}
			queryVo.setBannerList(bannerList);
		}
		// 推荐的几个分类
		BsAppIndexCatPageQueryOrder catOrder = new BsAppIndexCatPageQueryOrder();
		catOrder.setSerialNo(order.getSerialNo());
		catOrder.setCatStatus(YNEnum.Y);
		// 取8个
		catOrder.setPage(new PageEntity<>(1, 8));
		BsAppIndexCatPageQueryResult catResult = appIndexCatFacade.pageQuery(catOrder);
		List<BsAppIndexCatInfo> catTempList = catResult.getPageInfo().getList();
		for (BsAppIndexCatInfo bsAppIndexCatInfo : catTempList) {
			BsAppIndexCatVo vo = new BsAppIndexCatVo();
			BeanUtils.copyProperties(bsAppIndexCatInfo, vo);
			catList.add(vo);
		}
		queryVo.setCatList(catList);

		// 服务说明
		queryVo.setServiceNote("服务说明：目前配送区域主要为渝北区/江北区，不在配送区域范围的客户，请等待我们新增配送区域范围后下单；收货后24小时内可以申请退货；感谢您的支持！");

		queryVo.setMpTitle("鲜掌柜");
		// 两张通栏广告信息
		order.setBannerType(BannerTypeEnum.IDXHOR);
		order.setSerialNo(order.getSerialNo());
		BannerListQueryResult idxBannerResult = bannerManageFacade.bannerListQuery(order);
		// 2个广告位
		List<BsAppHorCatVo> idxVo = new ArrayList<>();
		if (idxBannerResult.getList().size() > 0) {
			List<BannerPageQueryInfo> tempList = idxBannerResult.getList();
			for (int i = 0; i < tempList.size(); i++) {
				BsAppHorCatVo idx1 = new BsAppHorCatVo();
				idx1.setSkuLabelImage(tempList.get(i).getBannerImageUrl());
				idx1.setSkuLabel(tempList.get(i).getBannerName());
				idx1.setSkuLabelName(tempList.get(i).getBannerImageLinkUrl());
				idxVo.add(idx1);
			}
		}
		queryVo.setIdxList(idxVo);

		order.setBannerType(BannerTypeEnum.IDXPOP);
		order.setSerialNo(order.getSerialNo());
		BannerListQueryResult popBannerResult = bannerManageFacade.bannerListQuery(order);
		List<BannerPageQueryInfo> popList = popBannerResult.getList();
		if (popList != null) {
			queryVo.setPopList(popList);
		}

		// 4个图标
		List<BsAppHorCatVo> horVo = new ArrayList<>();
		BsAppHorCatVo vo1 = new BsAppHorCatVo();
		vo1.setSkuLabel("BUYED");
		// vo.setSkuLabelImage(idxBannerResult.getList().get(0).getBannerImageUrl());
		//vo1.setSkuLabelImage("Frcs7C3xiLke04KiFoL9X9SaVUhe?timestamp=1590978489");
		vo1.setSkuLabelImage("cgqd1-1?timestamp=" + new Date().getTime());
		vo1.setSkuLabelName("常购清单");
		horVo.add(vo1);
		BsAppHorCatVo vo4 = new BsAppHorCatVo();
		vo4.setSkuLabel("INSTRUCTION");
		// vo2.setSkuLabelImage(idxBannerResult.getList().get(1).getBannerImageUrl());
		vo4.setSkuLabelImage("yhxz2-2?timestamp=" + new Date().getTime());
		vo4.setSkuLabelName("用户须知");
		horVo.add(vo4);



		BsAppHorCatVo vo3 = new BsAppHorCatVo();
		vo3.setSkuLabel("REWARD");
		// vo2.setSkuLabelImage(idxBannerResult.getList().get(1).getBannerImageUrl());
		vo3.setSkuLabelImage("mrhb3-3?timestamp=" + new Date().getTime());
		vo3.setSkuLabelName("每日红包");
		horVo.add(vo3);

		BsAppHorCatVo vo2 = new BsAppHorCatVo();
		vo2.setSkuLabel("EXCELLENCE");
		// vo.setSkuLabelImage(idxBannerResult.getList().get(0).getBannerImageUrl());
		vo2.setSkuLabelImage("zyhy4-4?timestamp=" + new Date().getTime());
		vo2.setSkuLabelName("卓越会员");
		horVo.add(vo2);
		// }
		queryVo.setHorList(horVo);
		return JsonUtils.success(queryVo);
	}

	@GetMapping(value = "bannerProductListQuery")
	@ApiOperation(value = "根据bannerId查询banner或者广告位商品信息 fjl", notes = "查询banner或者广告位商品信息")
	public JsonResult<BsBannerProductQueryVo> bannerProductListQuery(
			@RequestParam(value = "bannerId") Integer bannerId) {

		BannerProductQueryOrder order = new BannerProductQueryOrder();
		order.setBannerId(bannerId);
		order.setSerialNo(AppUtils.newNo());
		BannerProductQueryResult result = bannerManageFacade.bannerProductQuery(order);

		return result.isSuccess() ? JsonUtils.success(result, BsBannerProductQueryVo.class)
				: JsonUtils.fail(result.getMessage());
	}



	@GetMapping(value = "getAftermarketPhone")
	public JsonResult<List<UserDetailsQueryResult>> getAftermarketPhone(
			@RequestParam(value = "mchId") String mchId) {
		List<UserDetailsQueryResult> result = userManageFacade.getAftermarketPhone(mchId);
		return JsonUtils.success(result);
	}


}
